import Vue from "vue";
import App from "./app.vue";
import router from "./router/index.js";
import store from "./store/index.js";
import { query } from "./plugins/index";

Vue.prototype.$query = query;

import Nprogress from "nprogress";
import "nprogress/nprogress.css";
import Vant from "vant";
import "vant/lib/index.css";
import { Lazyload } from "vant";
Vue.config.productionTip = false;
Vue.use(Vant);
Vue.use(Lazyload, {
  lazyComponent: true,
});
import Vuei18n from "vue-i18n";

Vue.use(Vuei18n);
const i18n = new Vuei18n({
  locale: localStorage.getItem("lang") || "zh-CN",
  //this.$i18n.locale // 通过切换locale的值来实现语言切换
  messages: {
    "zh-CN": require("./static/lang/zh"), // 中文语言包
    "en-US": require("./static/lang/en"), // 英文语言包
  },
});
store.commit("updateLang", { lang: i18n.locale });
i18n.name = {
  "zh-CN": "china",
  "en-US": "english",
};

router.afterEach((to, from) => {
  Nprogress.done();
  console.log(to, from);
  to.meta && query("tagName:title").html(to.meta.title);
});

router.beforeEach((to, from, next) => {
  Nprogress.start();
  console.log(to, from);
  let token = store.commit("getToken");
  if (!token && !/\/login/.test(to.path)) {
    next("/login");
  } else {
    next();
  }
});

new Vue({
  store,
  router,
  i18n,
  render: (h) => h(App),
}).$mount("#app");
